package cn.krund.lyc.hotlerobot.wifimanage;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import cn.krund.lyc.hotlerobot.R;
import cn.krund.lyc.hotlerobot.activity.ui.WifiFunctionActivity;

/**
 * Created by Office on 2015/10/21.
 */
public class WifiStateDialog extends Dialog {

    public static boolean delete = false;
    // Wifi管理类
    private WifiAdmin mWifiAdmin;
    private Context context;
    private ScanResult scanResult;
    private String wifiName;
    private int level;
    private String securigyLevel;
    private OnNetworkChangeListener onNetworkChangeListener;

    public WifiStateDialog(Context context, int theme) {
        super(context, theme);
        this.mWifiAdmin = new WifiAdmin(context);
    }

    private WifiStateDialog(Context context, int theme, String wifiName,
                            int singlStren, String securityLevl) {
        super(context, theme);
        this.context = context;
        this.wifiName = wifiName;
        this.level = singlStren;
        this.securigyLevel = securityLevl;
        this.mWifiAdmin = new WifiAdmin(context);
    }


    public WifiStateDialog(Context context, int theme, ScanResult scanResult
    ) {
        this(context, theme, scanResult.SSID, scanResult.level, scanResult.capabilities);
        this.context = context;
        this.scanResult = scanResult;
        this.mWifiAdmin = new WifiAdmin(context);
//        this.onNetworkChangeListener = onNetworkChangeListener;
    }


    private TextView tv_wifi_connected;//已连接的wifi名
    private TextView tv_conn_status;//wifi连接状态
    private TextView tv_signal_strength;//信号强度
    private TextView tv_security_level;//安全水平
    private TextView tv_ip_address;//ip地址
    private TextView tv_btn_cancel;//取消
    private TextView tv_btn_disconnect;//忘记wifi

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wifi_state_dialog);
        setCanceledOnTouchOutside(false);
        initView();
        setListener();
    }

    private void initView() {
        tv_wifi_connected = (TextView) findViewById(R.id.tv_wifi_connected);
        tv_conn_status = (TextView) findViewById(R.id.tv_conn_status);
        tv_signal_strength = (TextView) findViewById(R.id.tv_signal_strength);
        tv_security_level = (TextView) findViewById(R.id.tv_security_level);
        tv_ip_address = (TextView) findViewById(R.id.tv_ip_address);
        tv_btn_cancel = (TextView) findViewById(R.id.tv_btn_cancel);
        tv_btn_disconnect = (TextView) findViewById(R.id.tv_btn_disconnect);

        tv_wifi_connected.setText(wifiName);
        tv_conn_status.setText("已连接");
        tv_signal_strength.setText(mWifiAdmin.singlLevToStr(level));
        tv_security_level.setText(securigyLevel);
        tv_ip_address.setText(mWifiAdmin.ipIntToString(mWifiAdmin.getIpAddress()));


    }

    private void setListener() {
        tv_btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WifiStateDialog.this.dismiss();
            }
        });

        tv_btn_disconnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (WifiFunctionActivity.getInfoMap!= null&&WifiFunctionActivity.getInfoMap.size()>0) {
                    WifiFunctionActivity.getInfoMap.remove(scanResult.SSID);//将此wifi忘记
                    delete = true;
                    PutInfoToMap.putMap(context, "wifiInfo", WifiFunctionActivity.getInfoMap);
                }
                // 断开连接
                int netId = mWifiAdmin.getConnNetId();
                mWifiAdmin.disConnectionWifi(netId);
                WifiStateDialog.this.dismiss();
                delete = false;
            }
        });
    }

    @Override
    public void show() {
        WindowManager wm = (WindowManager) getContext().getSystemService(
                Context.WINDOW_SERVICE);
        Point size = new Point();
        wm.getDefaultDisplay().getSize(size);

        super.show();
        getWindow().setLayout((int) (size.x * 9 / 10),
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }
}
